import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Locale;
import java.util.Properties;

import javax.swing.JOptionPane;

public class Connect {
	public static Connection connect() {

		Connection con = null;
		Locale.setDefault(Locale.US);
		String mysqlDriverName = "com.mysql.jdbc.Driver";
		try {
			Class.forName(mysqlDriverName);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
		 e.printStackTrace();
		}
		try {
			Properties properties = new Properties();
			File propertyFile = new File("project.properties");
            try {
				properties.load(new FileReader(propertyFile));
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
 		        JOptionPane.showMessageDialog(null, "Файл не найден"+ e.getMessage());
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				JOptionPane.showMessageDialog(null, "Не удалось прочитать данные"+ e.getMessage());
				
				e.printStackTrace();
			}
			String info=properties.getProperty("info");
			String login=properties.getProperty("login");
			String password=properties.getProperty("password");
			con = DriverManager
					.getConnection(info,
							login, password);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			JOptionPane.showMessageDialog(null, "Соединение не создано"+ e.getMessage());
			
			e.printStackTrace();
		}
		return con;

	}
	
	
	public static void closeConn(Connection con,PreparedStatement st)
	{
		try{
			st.close();
			con.close();
		}
		catch(SQLException e){
			if((con==null)&&(st==null))
				JOptionPane.showMessageDialog(null, "Не удалось соединиться с базой"+ e.getMessage());
			
		}
	
	}
	
	
}
